﻿// <copyright file="TimesPeopleProfile.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2009-05-06</date>
// <summary>TimesPeopleProfile class definition</summary>

namespace NYTimes
{
    using System;
    using System.Xml.Linq;
    using NYTimes.Core;
    using NYTimes.Data;

    /// <summary>
    /// New York Times TimesPeople Profile API
    /// </summary>
    /// <remarks>
    /// <a href="http://developer.nytimes.com/docs/timespeople_api">this article</a>.
    /// </remarks>
    public class TimesPeopleProfile : Core.TimesPeople
    {
        #region Fields
        /// <summary>
        /// the profile
        /// </summary>
        private Data.Profile profile;
        #endregion

        /// <summary>
        /// Initializes a new instance of the TimesPeopleProfile class.
        /// </summary>
        public TimesPeopleProfile()
        {
            if (SampleData.IsInDesignTool)
            {
                var xml = SampleData.GetSampleData("TimesPeopleProfile.xml");

                this.Parse(xml);
            }
        }

        /// <summary>
        /// Initializes a new instance of the TimesPeopleProfile class.
        /// </summary>
        /// <param name="sourceProfile">the source profile with a valid User Id</param>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "called from value converter")]
        internal TimesPeopleProfile(Profile sourceProfile)
        {
            this.Profile = sourceProfile;
        }

        #region Properties
        /// <summary>
        /// Gets the "profile" data type
        /// </summary>
        public override string DataType
        {
            get { return "profile"; }
        }

        /// <summary>
        /// Gets the profile returned as a <see cref="NYTimes.Data.Profile"/> object.
        /// </summary>
        public Data.Profile Profile
        {
            get
            {
                return this.profile;
            }

            private set
            {
                this.profile = value;

                this.TriggerPropertyChanged("Profile");
            }
        }
        #endregion

        #region Methods

        /// <summary>
        /// Error Handler
        /// </summary>
        /// <param name="failure">the web client error</param>
        public override void OnException(Exception failure)
        {
            this.Profile = null;
        }

        /// <summary>
        /// Parse the result set
        /// </summary>
        /// <param name="resultSet">the result set</param>
        public override void ParseResultSet(XElement resultSet)
        {
            if (resultSet == null)
            {
                throw new ArgumentNullException("resultSet");
            }

            var profileElement = resultSet.Element("results").Element("profile");

            this.Profile = CreateProfile(profileElement);
        }
        #endregion
    }
}
